<?php
/**
 * Created by PhpStorm.
 * User: Jason
 * Date: 2017/7/27
 * Time: 17:16
 */

namespace Admin\Controller;

use Admin\Service\SheetCommentService;
use Admin\Service\UserService;

class SheetCommentController extends AdminController
{
    /**
     * 获取谱单评论
     */
    public function index($sheet_id=0)
    {
        $param = I('post.');
        if(!empty($sheet_id)) $param['sheet_id'] = $sheet_id;
        
        $service = new SheetCommentService();
        $comment = $service->getCommentByParam($param);

        //获取系统马甲号
        $userService = new UserService();
        $aliasList = $userService->getAliasUserInfo();
        $this->assign('_alias', $aliasList);
        $this->assign('_sheetId', $sheet_id);

        $this->assign("_name", trim($param['name']));
        $this->assign('_comment', $comment);
        $this->assign('meta_title', "曲谱评论列表");
        $this->display('index');
    }

    public function del($sheet_id = 0)
    {
        $id = array_unique((array)I('id',0));
        $count = count($id);

        $id = is_array($id) ? implode(',',$id) : $id;

        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }
        $map = array('id' => array('in', $id) );

        $Model = M('SheetComment');
        $data = array('is_delete'=>1);

        if($Model->where($map)->save($data)){
            //曲谱评论数 -n
            M('Sheet')->where(array('id'=>$sheet_id))->setDec('comment_count', $count);

            $this->success('删除成功');
        } else {
            echo $Model->getLastSql();
            $this->error('删除失败！');
        }
    }

    /**
     * 添加评论
     */
    public function addComment()
    {
        $param = I('post.');

        if (empty($param['remark']) || empty($param['uid'])) {
            $this->error('缺少必要参数');
        }
        try {
            $service = new SheetCommentService();
            $service->add( $param );
            $this->success('发表成功');
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }

    }
}